<template>
  <div>
    <!-- 顶部 -->
    <!-- <van-nav-bar title="面经登陆" /> -->
    <nav class="nav">
      <span class="back" @click="$router.go(-1)">&lt;</span> 账号登陆
    </nav>

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登陆</van-button>
      </div>
    </van-form>

    <span class="register">还没有账号，去注册～</span>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)

      const data = await login(values)
      console.log(data)
      setToken(data.body.token)

      this.$toast.success('登陆成功')
      this.$router.push('/')
    }
  }
}
</script>

<style scoped lang="less">
.nav {
  height: 44px;
  border-bottom: 1px solid #e4e4e4;
  line-height: 44px;
  text-align: center;
  background: #329a6b;
  color: #fff;
  .back {
    font-size: 18px;
    color: #fff;
    position: absolute;
    left: 10px;
    top: 0;
    transform: scale(1, 1.5);
  }
}
.register {
  margin-left: 95px;
}
</style>
